package com.example.ycq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.naming.event.ObjectChangeListener;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate=null;
    @Autowired
    private StringRedisTemplate stringRedisTemplate=null;

    @RequestMapping("/stringAndHash")
    public Map<String, Object> testStringAndHash(){
        redisTemplate.opsForValue().set("key1","value1");
        redisTemplate.opsForValue().set("int_key","1");
        stringRedisTemplate.opsForValue().set("int","1");
        //使用运算
        stringRedisTemplate.opsForValue().increment("int",1);//increment:增量1
        //获取底层jedis连接
        Jedis jedis=(Jedis)stringRedisTemplate.getConnectionFactory().getConnection().getNativeConnection();
        //减1操作，这个命令RedisTemplate不支持，所以我们先获取底层的连接再操作
        jedis.decr("int");
        Map<String,String> hash=new HashMap<String,String>();
        hash.put("field1","value1");
        hash.put("field2","field2");
        //存入一个散列数据类型
        stringRedisTemplate.opsForHash().putAll("hash",hash);
        //新增一个字段
        stringRedisTemplate.opsForHash().put("hash","field3","value3");
        //绑定散列操作的key，这样可以连续对同一个散列数据类型进行操作
        BoundHashOperations hashOps=stringRedisTemplate.boundHashOps("hash");
        //删除两个字段
        hashOps.delete("field1","field2");
        //新增一个字段
        hashOps.put("filed4","value5");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("success",true);
        return map;

    }


//    @RequestMapping("/list")
//    public Map<String,Object> testList(){
//        //插入两个链表，注意它们在链表的顺序
//        //链表从左到右顺序为v10,v8,v6,v4,v2
//        stringRedisTemplate
//
//
//
//    }
}
